home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C22 / MultipleInheritance2.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  884 b   |  43 lines

  1. //: C22:MultipleInheritance2.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Virtual base classes
  7. #include "../purge.h"
  8. #include <iostream>
  9. #include <vector>
  10. using namespace std;
  11.  
  12. class MBase {
  13. public:
  14.   virtual char* vf() const = 0;
  15.   virtual ~MBase() {}
  16. };
  17.  
  18. class D1 : virtual public MBase {
  19. public:
  20.   char* vf() const { return "D1"; }
  21. };
  22.  
  23. class D2 : virtual public MBase {
  24. public:
  25.   char* vf() const { return "D2"; }
  26. };
  27.  
  28. // MUST explicitly disambiguate vf():
  29. class MI : public D1, public D2 {
  30. public:
  31.   char* vf() const { return D1::vf();}
  32. };
  33.  
  34. int main() {
  35.   vector<MBase*> b;
  36.   b.push_back(new D1);
  37.   b.push_back(new D2);
  38.   b.push_back(new MI); // OK
  39.   for(int i = 0; i < b.size(); i++)
  40.     cout << b[i]->vf() << endl;
  41.   purge(b);
  42. } ///:~
  43.